<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>员工薪资信息管理</title>
<link rel="stylesheet" type="text/css"
	href="../assets/admin/layui/css/layui.css" />
<link rel="stylesheet" type="text/css"
	href="../assets/admin/css/admin.css" />
</head>

<body>
	<div class="page-content-wrap" id="salaryManage">
		<h5
			style="text-align: center; font-size: 20px; line-height: 25px; color: #666">员工薪资信息管理</h5>
		<hr>
		<div class="layui-form-item">
			<div class="layui-inline tool-btn">
				<button class="layui-btn layui-btn-small site-demo-active"
					data-type="userAdd">
					<i class="layui-icon">&#xe654;</i>
				</button>
			</div>
			<div class="layui-inline">
				<input type="text" id="empName" placeholder="请输入员工姓名"
					autocomplete="off" class="layui-input">
			</div>
			<div class="layui-inline">
				<input type="text" class="layui-input" 
						id="salaryMonth" name="salaryMonth" placeholder="请选择薪资月份">
			</div>
			
			<button class="layui-btn layui-btn-normal site-demo-active"
				data-type="search" id="searchInfo">搜索</button>
			<table class="layui-hide" id="salary" lay-filter="salary"></table>

		</div>


		<hr></hr>
		<h5 style="text-align: center;">@2019.HR</h5>
	</div>
	<script src="../assets/admin/layui/layui.all.js" type="text/javascript"
		charset="utf-8"></script>
	<script>
		layui.use(['table','form', 'layedit', 'laydate'], function() {
			var $ = layui.jquery
			var table = layui.table;
			var laydate = layui.laydate;
			//成立日期
		       laydate.render({
					elem : '#salaryMonth'
		            ,format:'yyyy-MM'
		            ,type:'month'
		            ,trigger: 'click'
		        });
			table.render({
				id:'salaryList',
				elem : '#salary',
				url : '/salary/getAll',
				title : '用户数据表',
				totalRow : true,
				cols : [ [
					
					{
						field : 'salaryId',
						title : '部门编号'
					},
					{
						field : 'empName',
						title : '员工姓名'
					},
					{
						field : 'baseSalary',
						title : '基础工资'
					}
					,
					{
						field : 'achievements',
						title : '绩效工资'
					},
					{
						field : 'bonus',
						title : '项目奖金'
					},
					{
						field : 'checkDeduction',
						title : '考核扣除'
					},
					{
						field : 'sumSalary',
						title : '应发工资'
					}
					,
					{
						field : 'salaryMonth',
						title : '薪资月份'
					},{
						field : 'salaryStatus',
						title : '薪资状态',
						templet : function(res) {
							var salaryStatus=res.salaryStatus;
							if(1==salaryStatus){
								return '<em>未发放</em>'
							}else if(2==salaryStatus){
								return '<em>已发放</em>'
							}
						}
					},
						{
							fixed : 'right',
							title : '操作',
							width : 150,
							templet : function(res) {
								var salaryStatus=res.salaryStatus;
								if(1==salaryStatus){
									return '<a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a><a class="layui-btn layui-btn-normal  layui-btn-sm " lay-event="save">发放</a>'
								}else if(2==salaryStatus){
									return '<em>无操作</em>'
								}
							}
						} ] ],
				page : true
			});
			//事件绑定
			var active = {
				userAdd : function() {
						
						layer.open({
						      type: 2,
						      title: '添加薪资单',
						      shadeClose: true,
						      shade: false,
						   /*    maxmin: true,  *///开启最大化最小化按钮
						      area: ['800px', '400px'],
						      content: '/salary/salary-add.html'
						    });
					
					},
				search:function(){
					var empName=$("#empName").val();
					var salaryMonth=$("#salaryMonth").val();
					table.reload('salaryList',{
							method: 'POST'
						 , where: {"empName":empName,"salaryMonth":salaryMonth},page: {
						    curr: 1 //重新从第 1 页开始
						  }
						 
						})
				}
			};
			//表格事件绑定

		table.on('tool(salary)', function(obj) {
			var data = obj.data;
			if (obj.event === 'edit') {
				edit(data);
			} else if (obj.event === 'save') {
				layer.confirm('确定要发放吗，不可撤回！', function(index) {
					save(data,index);
				});
			}
		});
		function save(data,index){
			var param=data;
			param.salaryStatus=2;
			 $.ajax({
	                //几个参数需要注意一下
	                    type: "POST",//方法类型
	                    dataType: "json",//预期服务器返回的数据类型
	                    url: "/salary/updateSalary",//url
	                    data: param,
	                    success: function (result) {
	                    	if (result.code == "0") {
	                    			layer.close(index);
	                    			layer.msg("薪资发放成功");
	                    			table.reload('salaryList');
			                    }else{
			                    	layer.msg(result.msg);
			                    }
	                    },
	                    error : function() {
	                    	layer.msg('服务器错误', {
	                            icon: 5
	                        });
	                    }
	                });
		}
		function edit(data){
			var editFrom = layui.layer.open({
		        title : "编辑薪资信息",
		        type : 2,
		        content : "/salary/salary-edit.html",//弹出层页面
		        area: ['800px', '400px'],
		        success : function(layero, index){
		            var body = layui.layer.getChildFrame('body', index);
		            //获取窗口对象
		            var iframeWindow = layero.find('iframe')[0].contentWindow;
		            body.find("#salaryId").val(data.salaryId);
		            body.find("#baseSalary").val(data.baseSalary);
		            body.find("#achievements").val(data.achievements);
		            body.find("#bonus").val(data.bonus);
		            body.find("#checkDeduction").val(data.checkDeduction);
		            body.find("#salaryMonth").val(data.salaryMonth.substring(0,7));
		            var empId=data.empId;
		          //加载员工选择下拉框
					  $.ajax({
			           //几个参数需要注意一下
			               type: "POST",//方法类型
			               dataType: "json",//预期服务器返回的数据类型
			               url: "/achives/getAllToSelect",//url
			               data: {},
			               success: function (result) {
			               	if (result.code == "0") {
			               		var data=result.data;
			               	  body.find("#empDepart").html('<option value="">请输入/选择员工</option>');
			               		if(data.length>0){
			               			for(i=0;i<data.length;i++){
			               				var temp=data[i];
			               				if(temp.empId==empId){
					               			  body.find("#empId").append('<option value="'+temp.empId+'" selected>'+temp.empName+'(工号:'+temp.empId+')</option>');
			               				}else{
					               			  body.find("#empId").append('<option value="'+temp.empId+'">'+temp.empName+'(工号:'+temp.empId+')</option>');

			               				}
			               			}
			               			iframeWindow.layui.form.render('select');
			               		}
			               		
			                 }else{
			                 	layer.msg('加载员工信息下拉框失败', {
			                         icon: 5
			                     });
			                 }
			               },
			               error : function() {
			               	layer.msg('加载员工信息下拉框失败', {
			                       icon: 5
			                   });
			               }
			           });
		            iframeWindow.layui.form.render();
		        }});
		}
		$('#salaryManage .layui-btn').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});

	});
	</script>
</body>

</html>